package com.gmcc.docmanager.setting;

import com.gmcc.docmanager.R;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;

public class SettingActivity extends PreferenceActivity implements
		OnSharedPreferenceChangeListener {
	public static final String ACTION_START = "com.gmcc.docmanager.setting.systemsetting";

	private static final String TAG = "SettingActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.setting_preferences);

		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(this);
		prefs.registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		Log.d(TAG, "onSharedPreferenceChanged: " + key);
		if (key.equals(SettingField.PREF_DOWNLOAD_TASKCOUNT_SIMULTANEOUSLY)) {
			String taskCount = sharedPreferences.getString(key, getResources()
					.getString(R.string.default_download_simultaneously));

			Intent intent = new Intent(SettingField.ACTION_TASKCOUNT_CHANGED);
			int count = Integer.parseInt(taskCount);
			intent.putExtra(SettingField.TAG_RUN_COUNT, count);

			sendBroadcast(intent);
		} else if (key.equals(SettingField.PREF_DOWNLOAD_HISTORY)) {
			String historyCount = sharedPreferences.getString(key,
					getResources().getString(
							R.string.default_download_history_count));

			Intent intent = new Intent(SettingField.ACTION_HISTORYCOUNT_CHANGED);
			int count = Integer.parseInt(historyCount);
			intent.putExtra(SettingField.TAG_HISTORY_COUNT, count);

			sendBroadcast(intent);
		}
	}
}
